import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

// 课程类
class Course {
    private String courseId; // 课程编号
    private String name; // 课程名称
    private int capacity; // 课程容量
    private List<Student> students; // 已选课学生列表

    public Course(String courseId, String name, int capacity) {
        this.courseId = courseId;
        this.name = name;
        this.capacity = capacity;
        this.students = new ArrayList<>();
    }

    public String getCourseId() {
        return courseId;
    }

    public String getName() {
        return name;
    }

    public int getCapacity() {
        return capacity;
    }

    public List<Student> getStudents() {
        return students;
    }

    public boolean enrollStudent(Student student) {
        if (students.size() < capacity) {
            students.add(student);
            return true;
        } else {
            return false;
        }
    }
}

// 学生类
class Student {
    private String studentId; // 学生学号
    private String name; // 学生姓名
    private List<Course> courses; // 已选课程列表

    public Student(String studentId, String name) {
        this.studentId = studentId;
        this.name = name;
        this.courses = new ArrayList<>();
    }

    public String getStudentId() {
        return studentId;
    }

    public String getName() {
        return name;
    }

    public List<Course> getCourses() {
        return courses;
    }

    public void enrollCourse(Course course) {
        courses.add(course);
    }
}

// 选课系统类
class CourseSelectionSystem {
    private Map<String, Course> courses; // 课程字典，用课程编号作为键，课程对象作为值
    private Map<String, Student> students; // 学生字典，用学号作为键，学生对象作为值

    public CourseSelectionSystem() {
        this.courses = new HashMap<>();
        this.students = new HashMap<>();
    }

    public void addCourse(String courseId, String name, int capacity) {
        Course course = new Course(courseId, name, capacity);
        courses.put(courseId, course);
    }

    public void addStudent(String studentId, String name) {
        Student student = new Student(studentId, name);
        students.put(studentId, student);
    }

    public boolean enrollStudentInCourse(String studentId, String courseId) {
        if (students.containsKey(studentId) && courses.containsKey(courseId)) {
            Student student = students.get(studentId);
            Course course = courses.get(courseId);
            if (course.enrollStudent(student)) {
                student.enrollCourse(course);
                return true;
            }
        }
        return false;
    }

    public String getCourseDetails(String courseId) {
        if (courses.containsKey(courseId)) {
            Course course = courses.get(courseId);
            int remainingCapacity = course.getCapacity() - course.getStudents().size();
            return "课程编号：" + course.getCourseId() + "\n课程名称：" + course.getName() +
                    "\n剩余数量：" + remainingCapacity;
        }
        return null;
    }

    public Map<String, Course> getCourses() {
        return courses;
    }

    public Map<String, Student> getStudents() {
        return students;
    }
}

// 示例用法
public class Main {
    public static void main(String[] args) {
        CourseSelectionSystem system = new CourseSelectionSystem();
        system.addCourse("1", "数学", 50);
        system.addCourse("2", "英语", 30);
        system.addCourse("4", "马原", 20);

        system.addStudent("1", "小黑子");
        system.addStudent("2", "小白子");
        system.addStudent("3", "绿卡");

        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("请选择操作：");
            System.out.println("1. 查询课程");
            System.out.println("2. 查询学生所选的课程");
            System.out.println("3. 通过选课查询学生");
            System.out.println("4. 学生选课");
            System.out.println("0. 退出");

            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("请输入课程编号：");
                    String courseId = scanner.next();
                    String courseDetails = system.getCourseDetails(courseId);
                    if (courseDetails != null) {
                        System.out.println("课程详情：\n" + courseDetails);
                    } else {
                        System.out.println("找不到该课程！");
                    }
                    break;
                case 2:
                    System.out.println("请输入学生学号：");
                    String studentId = scanner.next();
                    if (system.getStudents().containsKey(studentId)) {
                        Student student = system.getStudents().get(studentId);
                        System.out.println("学生已选课程：");
                        for (Course course : student.getCourses()) {
                            System.out.println("课程编号：" + course.getCourseId() + "，课程名称：" + course.getName());
                        }
                    } else {
                        System.out.println("找不到该学生！");
                    }
                    break;
                case 3:
                    System.out.println("请输入课程编号：");
                    String courseToQuery = scanner.next();
                    if (system.getCourses().containsKey(courseToQuery)) {
                        Course course = system.getCourses().get(courseToQuery);
                        System.out.println("选修该课程的学生：");
                        for (Student student : course.getStudents()) {
                            System.out.println("学号：" + student.getStudentId() + "，姓名：" + student.getName());
                        }
                    } else {
                        System.out.println("找不到该课程！");
                    }
                    break;
                case 4:
                    System.out.println("请输入学生学号：");
                    String studentIdToEnroll = scanner.next();
                    System.out.println("请输入课程编号：");
                    String courseIdToEnroll = scanner.next();
                    boolean success = system.enrollStudentInCourse(studentIdToEnroll, courseIdToEnroll);
                    if (success) {
                        System.out.println("选课成功！");
                    } else {
                        System.out.println("选课失败！课程已满或学生不存在。");
                    }
                    break;
                case 0:
                    System.out.println("感谢使用选课系统，再见！");
                    break;
                default:
                    System.out.println("无效的操作，请重新选择！");
                    break;
            }

            System.out.println();
        } while (choice != 0);
    }
}
